package com.sy.cloud.guess.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.sy.cloud.guess.model.Gokguessnum;
import com.sy.cloud.wechat.entity.WeChatUserInfo;
import com.sy.cloud.wechat.model.WechatUser;
import com.sy.cloud.wechat.service.AuthorizeService;
import com.sy.common.model.Juejinuser;
import com.sy.common.persistence.QueryHelper;
import com.sy.common.service.IBaseService;
import com.sy.common.service.ITestService;
import com.sy.shiro.UsernameToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuwenquan on 2017/9/11.
 * 默认首页
 */
@Controller
@Slf4j
public class IndexController {

    @Resource
    private IBaseService baseService;
    @Resource
    private ITestService testService;
    private AuthorizeService authorizeService = new AuthorizeService();

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response, Model model){
        // 获取公众号授权，把获取到的openid存储到session中
        String openId = request.getParameter("open_id");
        if( openId!= null ){
            request.getSession().setAttribute("openId", openId);
        }else{
            String token = request.getParameter("token");
            if( token != null ){

            }
        }



        QueryHelper queryHelper = new QueryHelper();
        queryHelper.setLimit(10);
        queryHelper.setOffset(0);
        List<Gokguessnum> goklist = baseService.listQueryForPage( "From Gokguessnum u order by u.create_time desc", Gokguessnum.class, queryHelper );

        JSONObject js = new JSONObject();
        JSONArray array = new JSONArray();

        model.addAttribute("chart", goklist );


        Juejinuser juejinuser = new Juejinuser();
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principalCollection = subject.getPrincipals();
        if( principalCollection!= null ){
            UsernameToken token = new UsernameToken();
            Object o1 = principalCollection.getPrimaryPrincipal();
            token = (UsernameToken)subject.getPrincipals().getPrimaryPrincipal();
            StringBuilder hql = new StringBuilder();
            hql.append("From Juejinuser j where phone='");
            hql.append( token.getPhone() );
            hql.append("'");
            List<Juejinuser> juejinuserList = baseService.listQuery( hql.toString(), Juejinuser.class);
            juejinuser = juejinuserList.get(0);
        }

        if( juejinuser.getId() != -1 ){
            model.addAttribute("user", juejinuser );
            model.addAttribute("money", juejinuser.getMoney().toString() );// 余额
            model.addAttribute("ifvip", juejinuser.getIfvip().toString() );// 是否vip
            model.addAttribute("star", juejinuser.getStar().toString() );// 获取几颗星
            model.addAttribute("knapsack", juejinuser.getKnapsack() );// 背包接口
        }else{

        }
        return "index";
    }

    @RequestMapping(value = "/game",method = RequestMethod.GET)
    public String test(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "game";

    }
    @RequestMapping(value = "/test.do",method = RequestMethod.POST)
    public void test1(int count) {
        log.info(String.valueOf(count));
        log.info("test2的值");
    }
}
